package com.potenza.media.controller;


import com.potenza.media.model.CheckChunkResult;
import com.potenza.media.service.MediaUploadService;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/media/upload")
public class MediaUploadController {

    @Autowired
    MediaUploadService mediaUploadService;

    @GetMapping("test")
    public  R test(){
        return R.ok();
    }
    //before-send-file
    @PostMapping("/register")
    public R register(@RequestParam("fileMd5") String fileMd5,
                      @RequestParam("fileName") String fileName,
                      @RequestParam("fileSize") Long fileSize,
                      @RequestParam("mimetype") String mimetype,
                      @RequestParam("fileExt") String fileExt) {
        SysUserEntity principal =(SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        System.out.println(principal);
        return mediaUploadService.register(fileMd5,fileName,fileSize,mimetype,fileExt);
    }

    @PostMapping("/checkchunk")
    public CheckChunkResult checkchunk(@RequestParam("fileMd5") String fileMd5,
                                       @RequestParam("chunk") Integer chunk,
                                       @RequestParam("chunkSize") Integer chunkSize) {
        return mediaUploadService.checkchunk(fileMd5,chunk,chunkSize);
    }

    @PostMapping("/uploadchunk")
    public R uploadchunk(@RequestParam("file") MultipartFile file,
                                      @RequestParam("fileMd5") String fileMd5,
                                      @RequestParam("chunk") Integer chunk) {
        SysUserEntity principal =(SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        System.out.println("uploadchunk:" + principal);
        return mediaUploadService.uploadchunk(file,fileMd5,chunk);
    }

    @PostMapping("/mergechunks")
    public R mergechunks(@RequestParam("fileMd5") String fileMd5,
                        @RequestParam("fileName") String fileName,
                        @RequestParam("fileSize") Long fileSize,
                        @RequestParam("mimetype") String mimetype,
                        @RequestParam("fileExt") String fileExt){
        return mediaUploadService.mergechunks(fileMd5,fileName,fileSize,mimetype,fileExt);
    }
}

